package com.bdc.whatwhat.task;

import android.os.AsyncTask;

import com.bdc.whatwhat.Service.Service;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;

import java.util.List;

/**
 * Created by florentchampigny on 25/06/14.
 */
public class MonProfilTask extends AsyncTask<Object,Void,Whatter>{

    public interface MonProfilTaskDelegate{
        public void afficherChargement(boolean afficher);
        public void onMonProfilRecu(Whatter monProfil);
        public void onErreurReseau();
    }

    MonProfilTaskDelegate delegate;
    boolean erreurReseau;

    public MonProfilTask(MonProfilTaskDelegate delegate){
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(delegate != null)
            delegate.afficherChargement(true);
    }

    @Override
    protected Whatter doInBackground(Object...objects) {
        Whatter monProfil = null;
        String token = (String) objects[0];

        try{
            return Service.monProfil(token);
        }catch (Service.NetworkException e){
            erreurReseau = true;
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Whatter profil) {
        super.onPostExecute(profil);
        if(delegate != null) {
            delegate.afficherChargement(false);
            if(erreurReseau)
                delegate.onErreurReseau();
            else
                delegate.onMonProfilRecu(profil);
        }
    }
}
